package com.wjpeng.wms.web.controller;

import com.wjpeng.wms.domain.Systemmenu;
import com.wjpeng.wms.query.SystemMenuQueryObject;
import com.wjpeng.wms.service.ISystemmenuService;
import com.wjpeng.wms.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("systemmenu")
public class SystemmenuController {
    @Autowired
    private ISystemmenuService systemmenuService;

    @RequestMapping("list")
    public String list(Model model,@ModelAttribute("qo") SystemMenuQueryObject qo){

        //显示当前菜单层级
        if (qo.getParentId() != null) {//如果传过来有子菜单的id
            //获取所有的父菜单
            List<Systemmenu> menus = systemmenuService.getParentMenus(qo.getParentId());
            model.addAttribute("menus",menus);
        }

        model.addAttribute("list",systemmenuService.query(qo));
        return "systemmenu/list";
    }

    @RequestMapping("input")
    public String input(Model model,Long id, Long parentId){
        if(id != null){
            model.addAttribute("entity",systemmenuService.get(id));
        }
        if(parentId != null){//如果传过来的参数不为null,则说明有父菜单,然后回显有父菜单的菜单信息
            model.addAttribute("parent",systemmenuService.get(parentId));
        }
        return "systemmenu/input";
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){
        systemmenuService.delete(id);
        return new JSONResult();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Systemmenu systemmenu){
        systemmenuService.saveOrUpdate(systemmenu);
        return new JSONResult();
    }


    /**
     * [{id:2,pId:1, name:"部门管理", action:"/department/list.do"}
     *  {id:3,pId:1, name:"员工管理", action:"/employee/list.do"}]
     * @param menuSn
     * @return
     * @throws Exception
     */
    @RequestMapping("getMenusBySn")
    @ResponseBody
    public Object getMenusBySn(String menuSn) throws  Exception{ //获取sn这个请求参数
        List<Map<String,Object>> list = systemmenuService.getMenusBySn(menuSn);//定义一个List集合,存放map集合
        return list;
    }


}
